
 /**
 * CharacterEncodingFilter.java.java.
 * User: BrianChen Date: 2012-7-29 下午12:32:49
 *
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * All rights reserved.
 */
package com.collapsar.core.aop.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <pre>
 * REVISIONS: 
 * Version 	Date 		Author 			Description
 * ------------------------------------------------------------------- 
 * 1.0 		2012-7-29 	BrianChen 	1. Created this class. 
 * </pre>
 * 
 * @author BrianChen
 * @date 2012-7-29 下午12:32:49
 * @version 1.0
 * @see
 * 
 */
public class CharacterEncodingFilter implements Filter{
	private static final Logger logger = LoggerFactory.getLogger(CharacterEncodingFilter.class);
	/**
	 * The default character encoding to set for requests that pass through this filter.
	 */
	protected String encoding = null;
	/**
	 * Should a character encoding specified by the client be ignored?
	 */
	protected boolean ignore = true;
	/**
	 * The filter configuration object we are associated with. If this value is
	 * null, this filter instance is not currently configured.
	 */
	protected FilterConfig filterConfig = null;
	
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		this.filterConfig = filterConfig;
		this.encoding = filterConfig.getInitParameter("encoding");
		String value = filterConfig.getInitParameter("ignore");
		if (value == null)
			this.ignore = true;
		else if (value.equalsIgnoreCase("true"))
			this.ignore = true;
		else if (value.equalsIgnoreCase("yes"))
			this.ignore = true;
		else
			this.ignore = false;
	}
	
	@Override
	public void destroy() {
		this.encoding = null;
		this.filterConfig = null;
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		// Conditionally select and set the character encoding to be used
		if (this.ignore || (request.getCharacterEncoding() == null)) {
			String encoding = selectEncoding(request);
			if (encoding != null)
				request.setCharacterEncoding(encoding);
		}
		
		String requestURL = ((HttpServletRequest) request).getRequestURL().toString();
		logger.info("CharacterEncodingFilter filter URL:" + requestURL);

		// Pass control on to the next filter
		chain.doFilter(request, response);
	}
	
	/**
	 * Select an appropriate character encoding to be used, based on the
	 * characteristics of the current request and/or filter initialization
	 * parameters. If no character encoding should be set, return<code>null</code>.
	 * <p>
	 * The default implementation unconditionally returns the value configured
	 * by the <strong>encoding</strong> initialization parameter for this filter.
	 * 
	 * @param request
	 *            The servlet request we are processing
	 */
	private String selectEncoding(ServletRequest request) {
		/**some logic process*/
		return this.encoding;
	}

}

